import csv
import os
from decimal import Decimal

class FutuAccountInfo:
    def __init__(self):
        #资产净值
        self.net_asset = Decimal(0)
        #证券市值
        self.stock_value = Decimal(0)
        #现金
        self.cash = Decimal(0)
        #最大购买力
        self.max_buying_power = Decimal(0)
        #风控状态
        self.risk_status = ""
        #剩余流动性
        self.remaining_liquidity = Decimal(0)
        #杠杆倍数
        self.leverage = Decimal(0)
        #追缴保证金
        self.margin_call = Decimal(0)
        #冻结资金
        self.frozen_funds = Decimal(0)
        #在途资产
        self.in_transit_assets = Decimal(0)
        #卖空购买力
        self.short_buying_power = Decimal(0)
        #多头市值
        self.long_market_value = Decimal(0)
        #空头市值
        self.short_market_value = Decimal(0)
        #可用资金
        self.available_funds = Decimal(0)
        #港元现金
        self.hkd_cash =  Decimal(0)
        #港元现金可提
        self.hkd_withdrawable =  Decimal(0)
    def __str__(self):
        return f"FutuAccountInfo(net_asset={self.net_asset}, stock_value={self.stock_value}, cash={self.cash}, " \
               f"max_buying_power={self.max_buying_power}, risk_status='{self.risk_status}', " \
               f"remaining_liquidity={self.remaining_liquidity}, leverage={self.leverage}, " \
               f"margin_call={self.margin_call}, frozen_funds={self.frozen_funds}, " \
               f"in_transit_assets={self.in_transit_assets}, short_buying_power={self.short_buying_power}, " \
               f"long_market_value={self.long_market_value}, short_market_value={self.short_market_value}, " \
               f"available_funds={self.available_funds}, hkd_cash={self.hkd_cash}, " \
               f"hkd_withdrawable={self.hkd_withdrawable})"

class FutuAccountCsvParser:
    def parse(self, csv_file):
        if not os.path.exists(csv_file):
            raise FileNotFoundError(f"The file {csv_file} was not found.")
        with open(csv_file, 'r', encoding='utf-8') as file:
            reader = csv.reader(file)
            key_value_map = {}
            for row in reader:
                if len(row) >= 2 and row[0].strip():
                    key_value_map[row[0]] = row[1]

        account_info = FutuAccountInfo()
        account_info.net_asset = self.parse_decimal(key_value_map.get("资产净值"))
        account_info.stock_value = self.parse_decimal(key_value_map.get("证券市值"))
        account_info.cash = self.parse_decimal(key_value_map.get("现金"))
        account_info.max_buying_power = self.parse_decimal(key_value_map.get("最大购买力"))
        account_info.risk_status = key_value_map.get("风控状态", "")
        account_info.remaining_liquidity = self.parse_decimal(key_value_map.get("剩余流动性"))
        account_info.leverage = self.parse_decimal(key_value_map.get("杠杆倍数"))
        account_info.margin_call = self.parse_decimal(key_value_map.get("追缴保证金"))
        account_info.frozen_funds = self.parse_decimal(key_value_map.get("冻结资金"))
        account_info.in_transit_assets = self.parse_decimal(key_value_map.get("在途资产"))
        account_info.short_buying_power = self.parse_decimal(key_value_map.get("卖空购买力"))
        account_info.long_market_value = self.parse_decimal(key_value_map.get("多头市值"))
        account_info.short_market_value = self.parse_decimal(key_value_map.get("空头市值"))
        account_info.available_funds = self.parse_decimal(key_value_map.get("可用资金"))
        account_info.hkd_cash = self.parse_decimal(key_value_map.get("港元现金"))
        account_info.hkd_withdrawable = self.parse_decimal(key_value_map.get("港元现金可提"))

        return account_info
    # @staticmethod
    # def parse_double(value):
    #     if value is None or value.strip() == "":
    #         return 0.0
    #     return float(value.replace(",", ""))

    def parse_decimal(self, value: str) -> Decimal:
        """
        解析十进制数
        :param value: 字符串值
        :return: 十进制数对象
        """
        try:
            return Decimal(value.replace(',', '').strip())
        except Exception:
            return Decimal(0)

if __name__ == '__main__':
    file = "../../test/data/report/20250608futu_account.csv"
    parser = FutuAccountCsvParser()
    account_info = parser.parse(file)
    print(account_info)